
function fillRequiredFields(form) {
    form.add({
        fieldLabel: "Currency",
        name: "currency",
        editable: false,
        triggerAction: 'all',
        allowBlank: false,
        mode: 'local',
        xtype: "combo",
        store: new Ext.data.ArrayStore({
            id: 0,
            fields: [
                                            'myId',
                                            'displayText'
                                        ],
            data: [['BYR', 'BYR'], ['USD', 'USD']]
        }),
        valueField: 'myId',
        displayField: 'displayText'
    });
    form.add({
        fieldLabel: "Amount",
        name: "amount",
        allowBlank: false,
        xtype: "numberfield",
        value: 0,
        minValue: 1,
        maxValue: 1000000000
    });

    form.add({
        hidden: true,
        id: 'paymentDetail',
        name: "paymentDetail",
        value: 0
    });
}

function fillForm(form, data) {

    removeElementsFromForm(form);
    for (var i = 0; i < data.length; i++) {
        var record = data[i];
        if (record.name != 'amount' && record.name != 'paymentDetail' && record.name != 'currency') {
            record.regexfromServer = record.regex;
            record.regex = new RegExp(record.regex);

            form.add(record);
        }
    }
    fillRequiredFields(form);
}

function fillFormForUpdate(form, data) {
    removeElementsFromForm(form);
    var fields = data;
    var amount = 0;
    var currency = "";

    for (var i = 0; i < fields.length; i++) {
        var record = fields[i];
        switch (record.name) {
            case 'amount':
                amount = record.value;
                break;
            case 'currency':
                currency = record.value;
                break;
        }
        if (record.name != 'amount' && record.name != 'dayOfMonth' && record.name != 'paymentDetail' && record.name != 'currency') {
            record.regex = new RegExp(record.regex);
            form.add(record);
        }
    }
    form.add({
        fieldLabel: "Currency",
        name: "currency",
        editable: false,
        triggerAction: 'all',
        allowBlank: false,
        mode: 'local',
        xtype: "combo",
        value: currency,
        store: new Ext.data.ArrayStore({
            id: 0,
            fields: [
                                                'myId',
                                                'displayText'
                    ],
            data: [['BYR', 'BYR'], ['USD', 'USD']]
        }),
        valueField: 'myId',
        displayField: 'displayText'
    });
    form.add({
        fieldLabel: "Amount",
        name: "amount",
        allowBlank: false,
        xtype: "numberfield",
        value: amount,
        minValue: 1,
        maxValue: 1000000000
    });

    form.add({
        hidden: true,
        id: 'paymentDetail',
        name: "paymentDetail",
        value: 0
    });
}



Ext.onReady(function () {

    function Run() {
        //Is new Payment
        if (!paymentTemplateDetail) {
            $.post('/Payments/DetailJSON', { id: paymentId, cardNumber: cardNumber }, function (data, resp, text) {
                fillForm(simple, data);
                simple.show();
                simple.doLayout();
            });
        } else {
            //Is quick Payment
            data = eval(paymentTemplateDetail);
            fillFormForUpdate(simple, data);
            simple.show();
            simple.doLayout();
        }
    }

    function payPayment() {
        var res = saveData(simple);
        var form = simple.getForm();
        $('#paymentDetail').attr('value', res);
        if (form.isValid()) {

            form.submit({
                //  url: simple.url,
                params: { 'paymentId': paymentId },
                success: function (form, action) {
                    if (action.result.error) {
                        Ext.Msg.alert('Failed', action.result.error ? action.result.error : 'No response');
                    } else {
                        var transferId = action.result.transferId;
                        $('#paymentForm #paymentDetail').attr('value', res);
                        $('#paymentForm #transferId').attr('value', transferId);
                        var amount = getValueByName(simple, 'amount');
                        var currency = getValueByName(simple, 'currency');
                        $('#paymentForm #paymentId').attr('value', paymentId);
                        $('#paymentForm #amount').attr('value', amount);
                        $('#paymentForm #currency').attr('value', currency);
                        $('#paymentForm').attr("action", "/Payments/Bill");
                        $('#paymentForm').submit();
                    }
                },
                failure: function (form, action) {
                    Ext.Msg.alert('Failed', action.result ? action.result.message : 'No response');
                }
            });
        }
    }

    function SavePayment() {
        var detail = saveData(simple);
        var form = simple.getForm();
        if (form.isValid()) {
            Ext.MessageBox.prompt('Name', 'Please enter your name:', function (btn, name) {
                if (btn != 'ok' || !name)
                    return;

                $.post('/Payments/SaveQuickPayment',
                                    {
                                        paymentId: $('#paymentId').val(),
                                        cardNumber: $('#cardNumber').val(),
                                        paymentDetail: detail,
                                        name: name
                                    },
                                    function (result) {
                                        if (result.success) {
                                            Ext.Msg.alert('Success', 'Save has been completed');
                                        } else {
                                            Ext.Msg.alert('Failed', result.error ? result.error : 'No response');
                                        }
                                    });
            });
        }
    }

    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'side';

    var paymentId = $("#paymentId").val();
    var cardNumber = $('#cardNumber').val();
    var paymentTemplateDetail = $('#paymentForm #paymentDetail').val();
    var simple = new Ext.FormPanel({
        border: true,
        labelWidth: 140,
        url: '/Payments/Pay',
        header: true,
        autoScroll: true,
        title: 'Fill Form for Pay',
        bodyStyle: 'padding:5px 5px 0',

        width: 550,
        defaults: { width: 230 },
        defaultType: 'textfield',
        renderTo: 'form_div',

        buttons: [{
            iconCls: 'icon-save',
            text: 'Pay',
            listeners: {
                click: payPayment
            }
        },
                {
                    iconCls: 'icon-save',
                    text: 'Save as QuickPayment',
                    listeners: {
                        click: SavePayment
                    }
                }
                ]
    });
    Run();

});